﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Windows.Forms;
using System.Drawing;
using System.Drawing.Drawing2D;

namespace MulticastTV
{
    public partial class Form1 : Form
    {
        string itema, itemb, time, diff;
        DateTime ItemTimeA, ItemTimeB;
        float percent = 0;
        Timer clockupdate = new Timer();
        Timer HideFsOsd = new Timer();

        public bool fsodshown = false;

        public void PopUpInit()
        {
            itema = "";
            itemb = "";
            time = DateTime.Now.ToString("HH:mm");
            diff = "";
            clockupdate.Interval = 1000;
            clockupdate.Tick += new EventHandler(clockupdate_Tick);
            clockupdate.Start();
            HideFsOsd.Interval = Properties.Settings.Default.EPGPopUpTimeOut;
            HideFsOsd.Tick += new EventHandler(HideFsOsd_Tick);
        }

        /// <summary>
        /// draw info
        /// </summary>
        /// <param name="iA">Item A - current show</param>
        /// <param name="iB">Item B - next show</param>
        /// <param name="dA">Item A - current show time</param>
        /// <param name="dB">Item B - next show time</param>
        public void PopUpupdateText(string iA, string iB, DateTime dA, DateTime dB, bool autoshow)
        {
            HideFsOsd.Stop();
            HideFsOsd.Start();
            fsodshown = true;
            itema = iA;
            itemb = iB;
            ItemTimeA = dA;
            ItemTimeB = dB;
            if (ItemTimeA == DateTime.MinValue)
            {
                itema = "";
                itemb = "";
                diff = "";
                percent = 0;
                panel4.Invalidate(panel4.ClientRectangle);
                panel4.Update();
                return;
            }
            try
            {
                if (itemb != "")
                {
                    double full = ItemTimeB.Subtract(ItemTimeA).TotalMinutes;
                    double elapsed = DateTime.Now.Subtract(ItemTimeA).TotalMinutes;
                    percent = ((float)elapsed / (float)full);
                    if (elapsed >=0)
                        diff = ItemTimeB.Subtract(DateTime.Now).TotalMinutes.ToString("0' min");
                    else
                        diff = ItemTimeB.Subtract(ItemTimeA).TotalMinutes.ToString("0' min");
                }
                else
                {
                    diff = "";
                    percent = 0;
                }

                panel4.Invalidate(this.ClientRectangle);
                panel4.Update();
                if (autoshow)
                    PopUpShowWindow();
            }
            catch { }
        }

        public void PopUpClear()
        {
            itema = "";
            itemb = "";
            diff = "";
            percent = 0;
            panel4.Invalidate(panel4.ClientRectangle);
            panel4.Update();
            return;
        }

        private void panel4_Paint(object sender, PaintEventArgs e)
        {
            Graphics g = e.Graphics;
            int test1 = e.ClipRectangle.Width;
            int test2 = this.Width;
            int test3 = e.ClipRectangle.Height;
            int test4 = this.Height;
            int width = test1;
            int height = test3;
            Rectangle rect_gray = new Rectangle((int)(width * 0.03f), (int)(height / 2.0f) - 2, width - (int)(width * 0.06f), 4);
            Rectangle rect_green = new Rectangle((int)(width * 0.03), (int)(height / 2.0f) - 2, (int)(percent * (width - (width * 0.06f))), 4);

            if (test1 != 0 && test2 != 0 && test3 != 0 && test4 != 0 && width != 0 && height != 0 && rect_gray.Width != 0 && rect_gray.Height != 0)
            {
                //show duration progress bar
                LinearGradientBrush lBrush_gray = new LinearGradientBrush(rect_gray, Color.FromArgb(40, 28, 192, 0), Color.FromArgb(40, 35, 246, 0), LinearGradientMode.Horizontal);
                g.FillRectangle(lBrush_gray, rect_gray);
                if (percent != 0.0f && !float.IsInfinity(percent) && rect_green.Width > 0)
                {
                    LinearGradientBrush lBrush_green = new LinearGradientBrush(rect_green, Color.FromArgb(255, 28, 192, 0), Color.FromArgb(255, 35, 246, 0), LinearGradientMode.Horizontal);
                    g.FillRectangle(lBrush_green, rect_green);
                }

                StringFormat stringFormat = new StringFormat();
                stringFormat.Alignment = StringAlignment.Far;
                stringFormat.LineAlignment = StringAlignment.Center;
                stringFormat.Trimming = StringTrimming.EllipsisCharacter;

                StringFormat stringFormat2 = new StringFormat();
                stringFormat2.Alignment = StringAlignment.Near;
                stringFormat2.LineAlignment = StringAlignment.Center;
                stringFormat2.FormatFlags = StringFormatFlags.LineLimit;
                stringFormat2.Trimming = StringTrimming.EllipsisCharacter;

                // clock
                Font ClockFont = new Font("Verdana", 0.1875f * (float)height, FontStyle.Bold);
                Rectangle clock = new Rectangle(0, 1, width - (int)(width * 0.03f), (int)(height / 2.0f) + 1);
                e.Graphics.DrawString(time, ClockFont, Brushes.White, clock, stringFormat);

                // how long
                Rectangle hlong = new Rectangle(0, (int)(height / 2.0f) + 1, width - ((int)(width * 0.03)), (int)((height) / 2.0f));
                e.Graphics.DrawString(diff, ClockFont, Brushes.White, hlong, stringFormat);

                // first
                Rectangle FirstItem = new Rectangle((int)(width * 0.03), 1, (int)(width * 0.75f), (int)((height) / 2.0f) + 1);
                e.Graphics.DrawString(itema, ClockFont, Brushes.White, FirstItem, stringFormat2);

                // second
                Rectangle SecondItem = new Rectangle((int)(width * 0.03), (int)((height / 2.0f)) + 1, (int)(width * 0.75f), (int)((height) / 2.0f));
                e.Graphics.DrawString(itemb, ClockFont, Brushes.White, SecondItem, stringFormat2);
            }
        }

        public void PopUpRestarthideOSD()
        {
            HideFsOsd.Stop();
            HideFsOsd.Start();
        }

        public void PopUpShowWindow()
        {
            PopUpRestarthideOSD();

            if (Properties.Settings.Default.FullScreen)
            {
                //lets calculate position 

                float width = ((float)Properties.Settings.Default.EpgPopUpWidthPercent / 100.0f) * this.Width; // width allways fixed relative to the screen width
                if (width > this.Width)
                    width = this.Width;

                float left = 1;

                if (Properties.Settings.Default.EpgPopUpAlign == "right")
                {
                    left = (this.Width - (int)Math.Round(width, 0))  - Properties.Settings.Default.EpgPopUpXOffset;
                    if (left <= 0)
                        left = 0;
                }
                else if (Properties.Settings.Default.EpgPopUpAlign == "center")
                {
                    left = left = ((float)this.Width - width) / 2.0f ;
                    if (left <= 0)
                        left = 0;
                }
                else if (Properties.Settings.Default.EpgPopUpAlign == "left")
                {
                    left = Properties.Settings.Default.EpgPopUpXOffset ;
                    if (left + width <= panel4.Width)
                        left = 0;
                }

                panel4.Location = new Point((int)left, this.Height - Properties.Settings.Default.EPGPopUpHeight - Properties.Settings.Default.EPGpopupYOffset );
                panel4.Size = new Size((int)width, Properties.Settings.Default.EPGPopUpHeight);
                panel4.Visible = true;
                panel4.BringToFront();

                fsodshown = true;
            }
        }

        public void PopUpHide()
        {
            panel4.Visible = false;
            fsodshown = false;
        }

        private void clockupdate_Tick(object sender, EventArgs e)
        {
            time = DateTime.Now.ToString("HH:mm");
        }

        private void HideFsOsd_Tick(object sender, EventArgs e)
        {
            PopUpHide();
            fsodshown = false;
        }
    }
}
